package com.atguigu.activiti;

import com.atguigu.ServiceAuthApplication;
import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName:Process2
 * Package:com.atguigu.activiti
 * Description:
 *
 * @Author:小飞机
 * @Create：2023/6/3 - 11:55
 * @Version: v1.0
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ServiceAuthApplication.class)
//并行必须两个人都完成才可以到下一个进行审批
public class Processdeom03 {

    //部署流程实例
    @Autowired
    private RepositoryService repositoryService;

    //开始新的流程实例
    @Autowired
    private RuntimeService runtimeService;

    //任务管理类
    @Autowired
    private TaskService taskService;

    //历史管理类
    @Autowired
    private HistoryService historyService;

    @Test
    public void deployProcess01() {
        // 流程部署
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process/qingjia003.bpmn20.xml")
                .name("请假003")
                .deploy();
        System.out.println(deploy.getId());
        System.out.println(deploy.getName());
    }
    //启动流程实例
    @Test
    public void test2(){
        Map<String, Object> map = new HashMap<>();

        ProcessInstance jiaban = runtimeService.startProcessInstanceByKey("qingjia003");
        System.out.println("jiaban = " + jiaban.getId());
        System.out.println("jiaban.getProcessDefinitionId() = " + jiaban.getProcessDefinitionId());

    }

    //查询代办任务--zhao6
    @Test
    public void findTask(){

        String assignee1 = "huanghe";
        String assignee2 = "wang5";
        String assignee3 = "wnagfang";
        List<Task> zhangsan = taskService.createTaskQuery()
                .taskAssignee(assignee1 )
//                .taskAssignee(assignee2)
//                .taskAssignee(assignee3)
                .list();
        for (Task task : zhangsan) {
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        }

    }

    //完成任务办理
    @Test
    public void completTask() {
        Task task = taskService.createTaskQuery()
                .taskAssignee("wangfang")  //要查询的负责人
                .singleResult();//返回一条


        //完成任务,参数：任务id
        taskService.complete(task.getId());
        System.out.println("finish");
    }

}
